package com.mirror.utils.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * 跑马灯的文本框.
 * <p>
 * Copyright: Copyright (c) 2013-5-15 下午4:33:09
 * <p>
 * Company: 北京幻方朗睿技术有限公司
 * <p>
 * Author: 李晖
 * <p>
 * Version: 1.0
 * <p>
 */
public class CustomMarqueeTextView extends TextView implements Runnable {

	/** 当前滚动的位置 */
	private int currentScrollX = 0;

	/** 是否停止 */
	private boolean isStop = false;

	/** 文本的真实宽度 */
	private int textWidth;

	private boolean isMeasure = false;

	public CustomMarqueeTextView(Context context) {
		super(context);
	}

	public CustomMarqueeTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public CustomMarqueeTextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		// 文字宽度只需获取一次就可以了
		if (!isMeasure) {
			getTextWidth();
			isMeasure = true;
		}
	}

	@Override
	public void setText(CharSequence text, BufferType type) {
		super.setText(text, type);
		this.startScroll();
	}

	/**
	 * 获取文字宽度
	 */
	private void getTextWidth() {
		Paint paint = this.getPaint();
		String str = this.getText().toString();
		textWidth = (int) paint.measureText(str);
	}

	private long stime = 0;

	@Override
	public void run() {
		if (this.textWidth == 0 || this.getWidth() == 0) {
			postDelayed(this, 200);
			return;
		} else if (this.getWidth() >= this.textWidth) {
			return;
		} else if (isStop) {
			return;
		}
		if (stime == 0) {
			stime = System.currentTimeMillis();
		}
		double dx = (System.currentTimeMillis() - stime) * 2.0 / 20;
		currentScrollX = (int) dx;
		scrollTo(currentScrollX, 0);

		if (getScrollX() >= this.textWidth) {
			currentScrollX = 0 - this.getWidth();
			stime = System.currentTimeMillis() - currentScrollX * 10;
			scrollTo(currentScrollX, 0);
		}
		postDelayed(this, 20);
	}

	/**
	 * 开始滚动
	 */
	public void startScroll() {
		isStop = false;
		this.removeCallbacks(this);
		post(this);
	}

	/**
	 * 停止滚动
	 */
	public void stopScroll() {
		isStop = true;
	}

	/**
	 * 从头开始滚动
	 */
	public void restartScroll() {
		currentScrollX = 0;
		startScroll();
	}
}
